from django.conf import settings
from django.db import models
from users.models import User
# Create your models here.
from meiduo_mall.utils.models import BaseModel
from itsdangerous import TimedJSONWebSignatureSerializer as TJS




class OAuthQQModel(BaseModel):

    user = models.ForeignKey("users.User",on_delete=models.CASCADE,verbose_name="用户")
    openid = models.CharField(max_length=64,db_index=True,verbose_name="openid")


    class Meta:
        db_table = "tb_oauth_qq"
        verbose_name = "QQ登陆用户数据"
        verbose_name_plural = verbose_name



    @staticmethod
    def decode_token(access_token):
        tjs = TJS(settings.SECRET_KEY, 300)
        try:
            data = tjs.loads(access_token)
        except:
            raise Exception('错误的access_token值')

        openid = data.get('openid', None)
        # 判断是否失效
        if not openid:
            raise Exception('access_token值失效')

        return openid





